package org.hackystat.devcathlon.ui.page.scoreboard.commentpanel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.commentary.Comment;
import org.hackystat.devcathlon.engine.match.Match;

/**
 * The Comments elements for the scoreboard.
 * 
 * @author Daniel Arakaki
 *
 */
public class FrontCommentPanel extends Panel implements Serializable {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Initialize sessions and add a label to scoreboard.
   * 
   * @param wicketId A Wicket ID for inheritance.
   * @param matchList A list of matches.
   */
  public FrontCommentPanel(String wicketId, List<Match> matchList) {
    super(wicketId);
    String noMatch = "";
    
    if (matchList.isEmpty()) {
      noMatch = "";
      add(new Label("matchNameHeader", ""));
      
      List<Comment> commentList = new ArrayList<Comment>();
      
      DataView commentView = new DataView("commentList", new ListDataProvider(commentList)) {

        /** Support serialization. */
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(Item item) {
          Comment commentItem = (Comment) item.getModelObject();
          item.add(new Label("comments", commentItem.getComment()));
        }
        
      };
      add(commentView);
    }
    
    else {
      Match match = matchList.get(0);
    
      add(new Label("matchNameHeader", match.getName()));
    
      List<Comment> commentList = new ArrayList<Comment>(match.getComments());
    
      DataView commentView = new DataView("commentList", new ListDataProvider(commentList)) {

      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(Item item) {
        Comment commentItem = (Comment) item.getModelObject();
        item.add(new Label("comments", commentItem.getComment()));
      }
      
    };
    add(commentView);
    }
  add(new Label("noMatch", noMatch));
  }

}
